﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace lcmyjobs.Web.enprise
{
    public partial class ListJobs :BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Application["webname"] != null)
                {
                    this.Title = string.Format("单位管理中心—职位管理：职位管理  {0}", Application["webname"].ToString());
                }
                int enterprise_bid = -1;

                if (Session["enterprise_bid"] != null)
                {
                    enterprise_bid = Convert.ToInt32(Session["enterprise_bid"]);

                    int type = 0;
                    if (Request["type"] != null)
                    {
                        type = Convert.ToInt32(Request["type"]);
                    }
                    this.HF_type.Value = type.ToString(); ;
                    Bind(enterprise_bid);

                }
                else
                {
                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "请先登陆！", "EnterpriseLogin.html");
                }

            }
        }

        private void Bind(int enterprise_bid)
        {
            lcmyjobs.BLL.enterprise_releasejob rb = new BLL.enterprise_releasejob();

            int count = 0;
            int type = Convert.ToInt32(this.HF_type.Value);

            DataSet ds = rb.Get_enterprise_releasejobtotal(enterprise_bid, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, type,out count);

            if (type == 1)  //正在招聘中的职位
            {
                this.lbl_zpzw.ForeColor = Color.FromName("#252525");
                this.lbn_setno.Visible = false;     //激活职位 不显示
                this.lbn_republish.Visible = false; //重新发布 不显示
            }
            else if (type == 2) // 过期职位
            {
                this.lbl_gxzw.ForeColor = Color.FromName("#252525");
                this.lbn_reset.Visible = false;     //刷新职位 不显示
                this.lbn_setno.Visible = false;     //激活职位 不显示
                this.lbn_setuse.Visible = false;    //屏蔽职位 不显示
            }
            else if (type == 3) // 屏蔽职位
            {
                this.lbl_pbzw.ForeColor = Color.FromName("#252525");
                this.lbn_setuse.Visible = false;    //屏蔽职位 不显示
                this.lbn_reset.Visible = false;     //刷新职位 不显示
                this.lbn_republish.Visible = false; //重新发布 不显示
            }
            else if (type == 4) //急聘
            {
                this.lbl_jpzw.ForeColor = Color.FromName("#252525");
            }
            if (ds.Tables[1].Rows.Count > 0)
            {
                this.lbl_title.Text = " " + ds.Tables[1].Rows[0]["name"].ToString();

            }
            if (ds.Tables[2].Rows.Count > 0)
            {
                this.lbl_zpzw.Text = ds.Tables[2].Rows[0][0].ToString();
            }
            if (ds.Tables[3].Rows.Count > 0)
            {
                this.lbl_gxzw.Text = ds.Tables[3].Rows[0][0].ToString();
            }
            if (ds.Tables[4].Rows.Count > 0)
            {
                this.lbl_pbzw.Text = ds.Tables[4].Rows[0][0].ToString();
            }
            if (ds.Tables[5].Rows.Count > 0)
            {
                this.lbl_jpzw.Text = ds.Tables[5].Rows[0][0].ToString();
            }

            this.AspNetPager1.RecordCount = count;

            this.GridView1.DataSource = ds.Tables[0];
            this.GridView1.DataBind();
            Page.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            int enterprise_bid = -1;
            if (Session["enterprise_bid"] != null)
            {
                enterprise_bid = Convert.ToInt32(Session["enterprise_bid"]);

                Bind(enterprise_bid);

            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                if (e.CommandArgument != null)
                {
                    int enterprise_rid = Convert.ToInt32(e.CommandArgument);

                    lcmyjobs.BLL.enterprise_releasejob eb = new BLL.enterprise_releasejob();

                    if (eb.Delete(enterprise_rid))
                    {
                        int enterprise_bid = -1;

                        if (Session["enterprise_bid"] != null)
                        {
                            enterprise_bid = Convert.ToInt32(Session["enterprise_bid"]);

                            Bind(enterprise_bid);

                        }
                    }

                }
            }
            if (e.CommandName == "upd")
            {
                if (e.CommandArgument != null)
                {
                    int enterprise_rid = Convert.ToInt32(e.CommandArgument);

                    Session["enterprise_rid"] = enterprise_rid;

                    Response.Redirect("ModifyJobs.html");

                }
            }
        }

        //刷新职位
        protected void lbn_reset_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (!string.IsNullOrEmpty(idlist))
            {
                lcmyjobs.BLL.enterprise_releasejob er = new BLL.enterprise_releasejob();

                if (er.UpdateWhere("reftime", DateTime.Now.ToString(), idlist))
                {
                    Maticsoft.Common.MessageBox.Show(this, "刷新职位成功！");
                    int enterprise_bid = -1;
                    if (Session["enterprise_bid"] != null)
                    {
                        enterprise_bid = Convert.ToInt32(Session["enterprise_bid"]);

                        Bind(enterprise_bid);

                    }
                }
            }
            else
            {
                //Maticsoft.Common.MessageBox.ShowAndRedirect(this, "请选择所要刷新的职位！", "ListJobs.html");
            }
        }

        //激活职位
        protected void lbn_setuse_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (!string.IsNullOrEmpty(idlist))
            {
                lcmyjobs.BLL.enterprise_releasejob er = new BLL.enterprise_releasejob();

                if (er.UpdateWhere("isuse", Boolean.TrueString, idlist))
                {
                    Maticsoft.Common.MessageBox.Show(this, "激活职位成功！");
                    int enterprise_bid = -1;
                    if (Session["enterprise_bid"] != null)
                    {
                        enterprise_bid = Convert.ToInt32(Session["enterprise_bid"]);

                        Bind(enterprise_bid);

                    }
                }
            }
            else
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "请选择所要刷新的职位！", "ListJobs.html");
            }
        }
        //屏蔽职位
        protected void lbn_setno_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (!string.IsNullOrEmpty(idlist))
            {
                lcmyjobs.BLL.enterprise_releasejob er = new BLL.enterprise_releasejob();

                if (er.UpdateWhere("isuse", Boolean.FalseString, idlist))
                {
                    Maticsoft.Common.MessageBox.Show(this, "屏蔽职位成功！");
                    int enterprise_bid = -1;
                    if (Session["enterprise_bid"] != null)
                    {
                        enterprise_bid = Convert.ToInt32(Session["enterprise_bid"]);

                        Bind(enterprise_bid);

                    }
                }
            }
            else
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "请选择所要刷新的职位！", "ListJobs.html");
            }
        }

        //激活职位
        protected void lbn_republish_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (!string.IsNullOrEmpty(idlist))
            {
                lcmyjobs.BLL.enterprise_releasejob er = new BLL.enterprise_releasejob();
                if (er.UpdateWhere("releasetime", DateTime.Now.ToString(), idlist))
                {
                    Maticsoft.Common.MessageBox.Show(this, "重新发布职位成功！");
                    int enterprise_bid = -1;
                    if (Session["enterprise_bid"] != null)
                    {
                        enterprise_bid = Convert.ToInt32(Session["enterprise_bid"]);

                        Bind(enterprise_bid);

                    }
                }
            }
            else
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "请选择所要重新发布的职位！", "ListJobs.html");
            }
        }

        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)this.GridView1.Rows[i].FindControl("cbo_rid");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (GridView1.DataKeys[i].Value != null)
                    {
                        idlist += GridView1.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

            }
        }

    }
}